package com.alibaba.otter.manager.biz.user.dal.ibatis;

import com.alibaba.otter.manager.biz.user.dal.dataobject.UserDO;
import com.alibaba.otter.manager.biz.user.dto.UserQueryDTO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * UserDao: 数据操作接口
 * <p>
 * 这只是一个减少手工创建的模板文件
 * 可以任意添加方法和实现, 更改作者和重定义类名
 * <p/>@author Powered By Fluent Mybatis
 */
public interface UserDao extends BaseMapper<UserDO> {

    long uniqueInsert(UserDO userDo);

    UserDO getAuthenticatedUser(@Param("name") String name, @Param("password") String password);

    int getCount(Map<String,Object> condition);

    boolean checkUnique(UserDO userDo);

    List<UserDO> listByCondition(Map<String, Object> condition);

    default Page<UserDO> userPage(UserQueryDTO userQueryDTO, Pageable pageable){
        QueryWrapper q = QueryWrapper.create()
                .like(UserDO::getDepartment, userQueryDTO.getDepartment(), StringUtils::isNotBlank)
                .like(UserDO::getUsername, userQueryDTO.getName(), StringUtils::isNotBlank)
                .like(UserDO::getRealName, userQueryDTO.getRealName(), StringUtils::isNotBlank)
                .eq(UserDO::getId, userQueryDTO.getId(), Objects::nonNull);
        return paginate(pageable.getPageNumber(), pageable.getPageSize(), q);
    }

}
